// package/pages/rechargeRecord/rechargeRecord.js
let myRequest = require('../../../common/script/myRequest.js');
let api = require('../../../common/script/api.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isEnd: true,
    isWithout: true,
    page: 1, // 页码
    pageSize: 10,
    listData: [],
  },
  getData() {
    const that = this;
    let data = {
      page: that.data.page,
      page_size: that.data.pageSize,
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.getMemberMoneyOrderList, data).then(res => {
      wx.hideLoading();
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
      if (res.data.status == 1) {
        if (res.data.result.order_list.data.length == 0) {
          that.setData({
            isWithout: false
          })
        } else {
          res.data.result.order_list.data.forEach(item => {
            if (item.order_status == 2) {
              item.order_status = '充值成功';
            } else if (item.order_status == 3) {
              item.order_status = '充值失败';
            } else {
              item.order_status = '充值中...';
            }
          })
          that.setData({
            listData: res.data.result.order_list.data
          })
        }
      } else {
        wx.showModal({
          content: res.data.msg,
          showCancel: false,
          confirmText: '我知道了',
          confirmColor: '#ff5151',
          success(e) {
            wx.navigateBack({
              delta: 1
            })
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    that.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    const that = this;
    wx.showNavigationBarLoading()
    that.setData({
      page: 1,
      isEnd: true,
      isWithout: true,
    })
    that.getData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const that = this;
    if (!that.data.isEnd) {
      return
    }
    let data = {
      page: that.data.page + 1,
      page_size: that.data.pageSize,
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.getMemberMoneyOrderList, data).then(res => {
      wx.hideLoading();
      if (res.data.status == 1) {
        if (res.data.result.order_list.data.length == 0) {
          wx.showToast({
            title: '没有更多了',
            icon: 'none',
            duration: 1000
          })
          that.setData({
            isEnd: false
          })
        } else {
          res.data.result.order_list.data.forEach(item => {
            if (item.order_status == 2) {
              item.order_status = '充值成功';
            } else if (item.order_status == 3) {
              item.order_status = '充值失败';
            } else {
              item.order_status = '充值中...';
            }
          })
          that.setData({
            listData: [...that.data.listData, ...res.data.result.order_list.data],
            page: that.data.page + 1
          })
        }
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})